<template>
<div class="reg">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <!-- 
        label="用户名" 左边文字
        prop="age" 验证规则
        v-model.number="ruleForm.age"

    -->
     <el-form-item label="旧密码" prop="phone">
        <el-input v-model="ruleForm.username"></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
    </el-form-item>
     <el-form-item label="确认密码" prop="password">
        <el-input type="password" v-model="ruleForm.password1" autocomplete="off"></el-input>
    </el-form-item>
  
    <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
     
    </el-form>
    </div>
</template>

<script>
    export default {
         data() {
     var validateUser = (rule, value, callback) => {
       
         //value就是输入的值
        if (value === '') {
          callback(new Error('请输入手机号'));
        } else {
           callback()
        }
      };
      var validatePass1 = (rule, value, callback) => {
        if (value !== this.ruleForm.password) {
          callback(new Error('两次密码不一致'));
        } else {
         
          callback();
        }
      
      };
      
      return {
        ruleForm: {
          username:"",
          password: '',
          password1:""
        },
        rules: {
          username:[{ validator: validateUser, trigger: 'blur' }],
          password: [
            { validator: validatePass1, trigger: 'blur' }
          ],
          password1: [
            { validator: validatePass1, trigger: 'blur' }
          ],
        
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              //验证成功  -- 自定义规则验证成功
            // alert('submit!');
            console.log(this.ruleForm);

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
    }
</script>

<style scoped>

</style>